<script setup>
import { ref } from 'vue'
import {
  getOrdersByUsername,
  selectOrdersByQueryinf,
  delOderService
} from '@/api/borrowOrder.js'
import { ElInput } from 'element-plus'
import { useUserStore } from '@/stores'
import weChatPayment from '../Payment/weChatPayment.vue'
const orderList = ref([])
const loading = ref(false)
const queryinf = ref('')
const dialog = ref()
const isQuery = ref(false)
const userStore = useUserStore()

const getOrdersByQueryinf = async () => {
  isQuery.value = true
  loading.value = true
  const res = await selectOrdersByQueryinf(queryinf.value, userStore.username)
  orderList.value = res.data.data
  console.log(orderList.value)
  loading.value = false
}

const getOrdersById = async () => {
  isQuery.value = true
  loading.value = true
  const res = await getOrdersByUsername(userStore.username)
  orderList.value = res.data.data
  loading.value = false
}

getOrdersById()

const onDelOrder = async (row, refund) => {
  await ElMessageBox.confirm(
    refund ? '你确认要取消借阅么，押金将按原路返回' : '你确认要取消借阅么',
    '温馨提示',
    {
      type: 'warning',
      confirmButtonText: '确认',
      cancelButtonText: '取消'
    }
  )
  await delOderService(row.id)
  ElMessage.success('取消成功')
  getOrdersById()
}

const toPay = async (row) => {
  dialog.value.open(row)
}

const onSuccess = () => {
  ElMessage.success('支付成功')
  getOrdersById()
}
</script>

<template>
  <page-container title="我的借阅订单">
    <template #extra>
      <ElInput
        v-model="queryinf"
        clearable
        style="width: 200px"
        placeholder="书籍名称或者书籍作者"
      ></ElInput>
      <el-button @click="getOrdersByQueryinf">查询借阅订单</el-button>
    </template>

    <el-table v-loading="loading" :data="orderList" style="width: 100%">
      <el-table-column type="index" label="序号" width="100"></el-table-column>
      <el-table-column prop="book.bookName" label="书籍名称"></el-table-column>
      <el-table-column prop="book.author" label="作者"></el-table-column>
      <el-table-column prop="book.position" label="位置"></el-table-column>
      <el-table-column prop="startTime" label="开始时间"></el-table-column>
      <el-table-column prop="endTime" label="结束时间"></el-table-column>
      <el-table-column prop="num" label="数量"></el-table-column>
      <el-table-column prop="deposit" label="押金"></el-table-column>
      <el-table-column prop="state" label="状态"></el-table-column>
      <el-table-column prop="kkey" label="密钥"></el-table-column>
      <el-table-column label="操作" width="200">
        <!-- row 就是 channelList 的一项， $index 下标 -->
        <template #default="{ row, $index }">
          <el-button
            plain
            type="success"
            @click="toPay(row, $index)"
            v-if="row.state === '待支付'"
            >去支付</el-button
          >
          <el-button
            plain
            type="danger"
            @click="
              row.state === '待支付' ? onDelOrder(row, 0) : onDelOrder(row, 1)
            "
            v-if="(row.state === '待借阅') | (row.state === '待支付')"
            >取消借阅</el-button
          >
        </template>
      </el-table-column>

      <template #empty>
        <el-empty description="没有数据" v-if="isQuery === true"></el-empty>
        <el-empty
          description="请输入书籍名称或者书籍作者的关键字查询借阅订单"
          v-if="isQuery === false"
        ></el-empty>
      </template>
    </el-table>
    <weChatPayment ref="dialog" @success="onSuccess"></weChatPayment>
  </page-container>
</template>

<style lang="scss" scoped></style>
